package com.xxx.yuedong.config;

import com.xxx.yuedong.common.consts.ActionConstants;
import com.xxx.yuedong.common.consts.QueueConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author HG
 * @version 1.0
 **/

/**
 * 提醒队列和交换机的创建
 */
@Configuration
public class EventRabbitConfig {
    private final static String LIKE = QueueConstants.LIKE;
    private final static String FOLLOW = QueueConstants.FOLLOW;
    private final static String COMMENT = QueueConstants.COMMENT;
    private final static String UPDATE = QueueConstants.UPDATE;
    private final static String EVENT_EXCHANGE = "eventExchange";
    private final static String ANNOUNCE = QueueConstants.ANNOUNCE;
    private final static String COMMENTED_RESPONSE = QueueConstants.COMMENTED_RESPONSE;

    // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
    // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
    // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
    //   return new Queue("TestQueue",true,true,false);

    //一般设置一下队列的持久化就好,其余两个就是默认false
    @Bean
    public Queue LikeQueue() {
        return new Queue(LIKE, true);
    }

    @Bean
    public Queue FollowQueue() {
        return new Queue(FOLLOW, true);
    }

    @Bean
    public Queue UpdateQueue() {
        return new Queue(UPDATE, true);
    }

    @Bean
    public Queue CommentQueue() {
        return new Queue(COMMENT, true);
    }

    @Bean
    public Queue AnnounceQueue() {
        return new Queue(ANNOUNCE, true);
    }

    @Bean
    public Queue CommentResponseQueue() {
        return new Queue(COMMENTED_RESPONSE, true);
    }

    //创建主题交换机
    @Bean
    TopicExchange eventExchange() {
        return new TopicExchange(EVENT_EXCHANGE, true, false);
    }

    //将队列和交换机绑定，并设置用于匹配的路由键
    @Bean
    Binding bindingExchange1() {
        return BindingBuilder.bind(LikeQueue()).to(eventExchange()).with(ActionConstants.LIKED);
    }

    @Bean
    Binding bindingExchange2() {
        return BindingBuilder.bind(CommentQueue()).to(eventExchange()).with(ActionConstants.COMMENTED);
    }

    @Bean
    Binding bindingExchange3() {
        return BindingBuilder.bind(UpdateQueue()).to(eventExchange()).with(ActionConstants.UPDATED);
    }

    @Bean
    Binding bindingExchange4() {
        return BindingBuilder.bind(FollowQueue()).to(eventExchange()).with(ActionConstants.FOLLOWED);
    }

    @Bean
    Binding bindingExchange5() {
        return BindingBuilder.bind(AnnounceQueue()).to(eventExchange()).with(ActionConstants.ANNOUNCE);
    }

    @Bean
    Binding bindingExchange6() {
        return BindingBuilder.bind(CommentResponseQueue()).to(eventExchange()).with(ActionConstants.COMMENTED_RESPONSE);
    }
}
